﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 人员去向补录登记
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class LeaveAdditionController : AdminBaseController
    {
       /// <summary>
        /// 人员去向补录登记首页
        /// </summary>
        /// <param name="FClass"></param>
        /// <returns></returns>
        [LogPageActionFilter("人员去向补录登记", Order = 2)]
        public ActionResult Index(string FClass)
        {
            string strwhere = "FIsAdditonIns='Y' ";
            if (FClass != null)
            {
                strwhere += " and FLeaveReasonCd in (select FLeaveReasonCd from dbo.t_cdLeaveReason where FClass='" + FClass + "') "; 
            }

            BLL.HREmpl HREmplBLL = new BLL.HREmpl();

            if (OperateSession.get(Common.GlobalConstant.PARAM_LOGINID) == "admin")
            {
                ViewData["isVisible"] = true;
            }
            else
            {
                ViewData["isVisible"] = HREmplBLL.isFPositionProp(OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO));
            }
            ViewData["FClass"] = FClass;
            List<Model.Leave> LeaveList = GetLeaveList(strwhere, 1);
            return View(LeaveList);
        }

        public ActionResult LeaveListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();
            string FClass = Request.Params["FClass"];

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "FIsAdditonIns='Y' ";
            if (FClass != null)
            {
                strwhere += " and FLeaveReasonCd in (select FLeaveReasonCd from dbo.t_cdLeaveReason where  FClass='" + FClass + "')";
            }
            List<Model.Leave> LeaveList = GetLeaveList(strwhere, pageIndex);

            BLL.HREmpl HREmplBLL = new BLL.HREmpl();
            if (OperateSession.get(Common.GlobalConstant.PARAM_LOGINID) == "admin")
            {
                ViewData["isVisible"] = true;
            }
            else
            {
                ViewData["isVisible"] = HREmplBLL.isFPositionProp(OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO));
            }
            return PartialView("LeaveListPartial", LeaveList);
        }


        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.Leave> GetLeaveList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.Leave LeaveBLL = new BLL.Leave();
            string fiedls = "*";
            int total;
            
            DataSet ds = commonBll.GetListByPage("v_Leave", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FLeaveID", 1, out total);
            List<ElingOA6_Admin_DSJCJ.Model.Leave> LeaveList = LeaveBLL.DataTableToList(ds.Tables[0]);
            List<Model.Leave> LeaveAllList = new List<Model.Leave>();

            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                LeaveAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (LeaveList.Count > i)
                {
                    LeaveAllList[((pageIndex - 1) * pageRowSize) + i] = LeaveList[i];
                }
            }

            return LeaveAllList;
        }


        /// <summary>
        /// 删除定义的请假外出事由（单个）
        /// </summary>
        /// <param name="fWorkTimeID">请假外出事由编号</param>
        /// <returns>是否成功</returns>
        public string DelLeave(string FLeaveID)
        {
            try
            {
                BLL.Leave LeaveBLL = new BLL.Leave();
                LeaveBLL.Delete(decimal.Parse(FLeaveID));
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        /// <summary>
        /// 删除定义的请假外出事由（批量）
        /// </summary>
        /// <param name="workTimeNoArray">请假外出事由编号以逗号分隔</param>
        /// <returns>是否成功</returns>
        public string DeleteLeave(string LeaveNoArray)
        {
            try
            {
                BLL.Leave LeaveBLL = new BLL.Leave();
                LeaveBLL.DeleteList(LeaveNoArray);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        public string LeaveOut(string FLeaveID)
        {
            try
            {
                BLL.Leave LeaveBLL = new BLL.Leave();

                Model.Leave model = LeaveBLL.GetModel(decimal.Parse(FLeaveID));

                if (DateTime.Now < model.FBeginDate)
                {
                    return "date";
                }

                model.FSignOutDate = DateTime.Now;
                model.FSignOutEmpNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                model.FSignOutIP = Common.GenerateUtil.GetClientIPv4Address();
                model.FUpdDate = model.FSignOutDate;
                model.FUpdEmpNo = model.FSignOutEmpNo;
                model.FUpdIP = model.FSignOutIP;
                LeaveBLL.LeaveOut(model);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        public ActionResult Save(Model.Leave LeaveModel)
        {
            try
            {
                BLL.HREmplPosition HREmplBLL = new BLL.HREmplPosition();
                bool beginisnull = false;
                bool endisnull = false;
                BLL.Leave LeaveBLL = new BLL.Leave();
                LeaveModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                LeaveModel.FEmplNo = Common.Fetch.Post("hbFEmplNo");
                if (Fetch.Post("hdFClass") == "1")
                {
                    string begindate = Fetch.Post("FBeginDate");
                    if (begindate == "")
                    {
                        begindate = DateTime.Parse(Fetch.Post("hdFBeginDate")).ToString("yyyy-MM-dd");
                    }
                    if (begindate == "")
                    {
                        beginisnull = true;
                    }
                    else
                    {
                        string beginxz = Fetch.Post("BeginXZ");
                        BLL.WorkTime WorkTimeBLL = new BLL.WorkTime();
                        begindate += " " + WorkTimeBLL.GetTime(begindate, "Bregin", beginxz);
                        string endate = Fetch.Post("FEndDate");
                        if (endate == "")
                        {
                            endisnull = true;
                        }
                        else
                        {
                            string endxz = Fetch.Post("EndXZ");
                            endate += " " + WorkTimeBLL.GetTime(endate, "End", endxz);
                            LeaveModel.FBeginDate = DateTime.Parse(begindate);
                            LeaveModel.FEndDate = DateTime.Parse(endate);
                        }
                    }
                }
                else
                {
                          string begindate = Fetch.Post("FBeginDate");
                          if (begindate != "")
                          {
                              LeaveModel.FBeginDate = DateTime.Parse(Fetch.Post("FBeginDate"));
                          }
                          else
                          {
                              if (Fetch.Post("hdFBeginDate") == "")
                              {
                                  beginisnull = true;
                              }
                              else
                              {
                                  LeaveModel.FBeginDate = DateTime.Parse(Fetch.Post("hdFBeginDate"));
                              }
                          }

                          if (Fetch.Post("FEndDate") == "")
                          {
                              endisnull = true;
                          }
                          else
                          {
                              LeaveModel.FEndDate = DateTime.Parse(Fetch.Post("FEndDate"));
                          }
                }

                if (LeaveModel.FEndDate < LeaveModel.FBeginDate)
                {
                    ViewData["jsAlert"] = "结束时间必须小于开始时间";
                }
                else if (beginisnull)
                {
                    ViewData["jsAlert"] = "开始时间必须填写";
                }
                else if (endisnull)
                {
                    ViewData["jsAlert"] = "结束时间必须填写";
                }
                else
                {
                    switch (Fetch.Post("hdMethod"))
                    {
                        case "ADD":
                            LeaveModel.FInsDate = DateTime.Now;
                            LeaveModel.FInsEmpNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                            LeaveModel.FInsIP = Common.GenerateUtil.GetClientIPv4Address();
                            LeaveModel.FIsAdditonIns = "Y";
                            LeaveBLL.Add(LeaveModel);
                            ViewData["jsAlert"] = "操作成功";
                            break;
                        case "Edit":
                            //if (LeaveModel.FEndDate < DateTime.Now)
                            //{
                            //    ViewData["jsAlert"] = "修改结束时间必须大于当前时间";
                            //}
                            //else
                            //{
                            //    //LeaveModel.FLeaveID = decimal.Parse(Fetch.Post("hdFLeaveID"));
                               
                            //}
                             LeaveModel.FUpdDate = DateTime.Now;
                                LeaveModel.FUpdEmpNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                                LeaveModel.FUpdIP = Common.GenerateUtil.GetClientIPv4Address();
                                LeaveBLL.Update(LeaveModel);
                                ViewData["jsAlert"] = "操作成功";
                            break;
                    }
                }

            }
            catch
            {
                ViewData["jsAlert"] = "系统异常";
            }

            if (LeaveModel.FBeginDate < DateTime.Now)
            {
                ViewData["EditFBeginDate"] = false;
            }
            else
            {
                ViewData["EditFBeginDate"] = true;
            }
            ViewData["hdMethod"] = Fetch.Post("hdMethod");
            BLL.HREmpl HREmplzBLL = new BLL.HREmpl();
            if (OperateSession.get(Common.GlobalConstant.PARAM_LOGINID) == "admin")
            {
                ViewData["isVisible"] = true;
            }
            else
            {
                ViewData["isVisible"] = HREmplzBLL.isFPositionProp(OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO));
            }
            BLL.CdLeaveReason CdLeaveReasonBLL = new BLL.CdLeaveReason();
            ViewData["FClass"] = Fetch.Post("hdFClass");
            List<Model.CdLeaveReason> CdLeaveReasonList = CdLeaveReasonBLL.GetModelList("FClass='" + Fetch.Post("hdFClass") + "'");
            ViewData["LeaveReasonList"] = CdLeaveReasonList;
            return View("LeaveEdit", LeaveModel);
        }


        public ActionResult LeaveEdit(string FClass, string FLeaveID)
        {
            Model.Leave Leave = new Model.Leave();
            BLL.CdLeaveReason CdLeaveReasonBLL = new BLL.CdLeaveReason();
            List<Model.CdLeaveReason> CdLeaveReasonList = CdLeaveReasonBLL.GetModelList("FClass='" + FClass + "'");
            ViewData["LeaveReasonList"] = CdLeaveReasonList;
            if (FLeaveID != null)
            {
                BLL.Leave LeaveBLL = new BLL.Leave();
                Leave = LeaveBLL.GetModel(decimal.Parse(FLeaveID));

                string ss = Leave.FBeginDate.ToString("HH:mm:ss");

                string dd = Leave.FBeginDate.ToString("yyyy-MM-dd");

                BLL.WorkTime WorkTimeBLL = new BLL.WorkTime();
                Model.WorkTime WorkTimeModel = WorkTimeBLL.GetModelByDate(dd);
                if (ss == WorkTimeModel.FAMBeginTimeStr)
                {
                    Leave.BeginXZ = "am";
                }
                else
                {
                    Leave.BeginXZ = "pm";
                }

                if (Leave.FBeginDate < DateTime.Now)
                {
                    ViewData["EditFBeginDate"] = false;
                }
                else
                {
                    ViewData["EditFBeginDate"] = true;
                }

                 ss = Leave.FEndDate.ToString("HH:mm:ss");

                 dd = Leave.FEndDate.ToString("yyyy-MM-dd");

              

                 WorkTimeModel = WorkTimeBLL.GetModelByDate(dd);
                if (ss == WorkTimeModel.FAMEndTimeStr)
                {
                    Leave.EndXZ = "am";
                }
                else
                {
                    Leave.EndXZ = "pm";
                }


                ViewData["hdMethod"] = "Edit";
                ViewData["jsAlert"] = "";
            }
            else
            {
                ViewData["hdMethod"] = "ADD";
                ViewData["jsAlert"] = "";
                Leave.FEmplNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                Leave.FEmplName = OperateSession.get(Common.GlobalConstant.PARAM_USERNAME);
                Leave.FDeptNo = OperateSession.get(Common.GlobalConstant.PARAM_DEPTNO);
                if (CdLeaveReasonList.Count > 0)
                {
                    Leave.FLeaveReasonCd = CdLeaveReasonList[0].FLeaveReasonCd;
                }
                ViewData["EditFBeginDate"] = true;
            }
            BLL.HREmpl HREmplBLL = new BLL.HREmpl();
            if (OperateSession.get(Common.GlobalConstant.PARAM_LOGINID) == "admin")
            {
                ViewData["isVisible"] = true;
            }
            else
            {
                ViewData["isVisible"] = HREmplBLL.isFPositionProp(OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO));
            }
            ViewData["FClass"] = FClass;
            
            return View(Leave);
        }

    }
}
